from abc import abstractmethod

from bs4 import BeautifulSoup

import FileUtil
import HtmlUtil


class Site:
    def __init__(self):
        self.file = ''
        self.page_format = ''
        self.post_selector = ''

    @abstractmethod
    def phasePage(self, response):
        pass


# 2 to 2321
class Site3DM(Site):
    def __init__(self):
        super().__init__()
        self.file = './data/Ex3DM.xlsx'
        self.page_format = 'https://dl.3dmgame.com/all_all_{}_time/'
        self.post_selector = 'div[class="listwrap"] > ul > li'

    def phasePage(self, response):
        rtn = ''
        bs = BeautifulSoup(response.content, "lxml")
        list_post = bs.select('div[class="listwrap"] > ul > li')
        # Post解析测试
        # print(list_post[9])
        file = FileUtil.File3DM(self.file)
        for i in range(2, len(list_post)):
            post = list_post[i]
            title_selector = post.select('div[class="bt"] > a')
            title_txt = title_selector[0].contents[0]
            # 获取到标题
            # print(title_txt)
            link_url = title_selector[0].attrs['href']
            # 取得详情链接
            # print(link_url)
            img_selector = post.select('img')
            img_url = img_selector[0].attrs['data-original']
            # 取得图片链接
            # print(img_url)

            # 使用详情链接构造图片名称并下载图片
            img_txt = link_url.split('/')[-1].split('.')[0]
            HtmlUtil.download_pic(int(img_txt) % 100, img_txt + '.jpg', img_url)

            tag_selector = post.select('li[class="li2"] > a')
            tag_txt = ''
            for i_tag in tag_selector:
                # print(i_tag.contents[0], end=',')
                tag_txt += i_tag.contents[0]
                tag_txt += ','  # 标签之间逗号分隔
            # 获取到标签
            # print(tag_txt)
            file.setPost([title_txt, link_url, img_txt, tag_txt])
        file.save()
        return rtn
